import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/enums/index.dart';
import 'package:geomancy/pages/home/item.dart';
import 'package:geomancy/providers/user_provider.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/tools/constant.dart';
import 'package:geomancy/tools/utils.dart';
import 'package:geomancy/widgets/common_text.dart';
import 'package:geomancy/widgets/common_title.dart';
import 'package:geomancy/widgets/date_picker/index.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  final List<Map<String, dynamic>> itemList = [
    {
      "icon": "assets/icons/home_item_cai.png",
      "content": "指房间的财运位，关联于屋主财运的促进以及财富的保护。",
      "path": "/position_tips",
      "source": PositionTipsSource.wealth
    },
    {
      "icon": "assets/icons/home_item_wen.png",
      "content": "指房间的文昌位，关联于屋主的学业与事业提升以及贵人的帮扶。",
      "path": "/position_tips",
      "source": PositionTipsSource.study
    },
    {
      "icon": "assets/icons/home_item_yuan.png",
      "content": "指房间的人缘位，关联于屋主身边的人际关系与个人感情运势。",
      "path": "/position_tips",
      "source": PositionTipsSource.relations
    },
    {
      "icon": "assets/icons/home_item_kang.png",
      "content": "指房间的安康位，关联于屋主及家人的身体健康。",
      "path": "/health_index",
      "source": ""
    },
  ];

  goToHandle(int index) {
    Utils.shake();
    final isLogin = Utils.getLoginState(context);
    if (isLogin) {
      //文昌和桃花需要记录出生日期
      if (index == 1 || index == 2) {
        final user = context.read<UserProvider>().getUserInfo;
        final birthday = user?.birthday?.split(' ')[0];
        DatePickerUtil.show(context,
          height: 570.w,
          defaultValue: birthday,
          header: Container(
            height: 100.w,
            alignment: Alignment.center,
            child: const CommonText(txt: '设置出生年月', weight: FontWeight.bold),
          ),
          getValue: (String value) {
            context.push(itemList[index]["path"], extra: {
              "source": itemList[index]["source"],
              "birthday": value
            });
          }
        );
      } else {
        context.push(itemList[index]["path"], extra: {
          "source": itemList[index]["source"]
        });
      }
    }
  }


  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: ColorStyle.pageDefaultColor,
      body: SafeArea(
        bottom: false,
        child: ListView(
          physics: const ClampingScrollPhysics(),
          padding: EdgeInsets.only(left: Constant.PAGEPADDING.w, right: Constant.PAGEPADDING.w, top: Constant.PAGEPADDING.w),
          children: [
            const CommonTitleWidget(txt: '择吉位'),
            Column(
              children: itemList.asMap().keys.map((index) {
                return Container(
                  key: ValueKey(index),
                  margin: EdgeInsets.only(top: 40.w),
                  child: HomeItemWidget(
                      iconPath: itemList[index]["icon"],
                      content: itemList[index]['content'],
                      onTap: () {
                        goToHandle(index);
                      }
                  ),
                );
              }).toList(),
            )
          ],
        ),
      )
    );
  }
}
